package com.maoslv.service.user;

import com.maoslv.mapper.PersonalMapper;
import com.maoslv.mapper.UserMapper;
import com.maoslv.pojo.Info;
import com.maoslv.pojo.Role;
import com.maoslv.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;

    @Autowired
    PersonalMapper personalMapper;

    //	获得发件人信息
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    JavaMailSender mailSender;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int registerUser(User user) {
        Integer lastInfoId = personalMapper.getLastInfoId();
        personalMapper.addInfo(lastInfoId + 1);
        user.setInfoId(lastInfoId + 1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.registerUser(user);
        return userMapper.autoAssignRole(user.getId());
    }

    @Override
    public User queryUsername(String username) {
        return userMapper.queryUsername(username);
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.getByUsername(username);
    }

    public String getUserAuthority(Integer id) {
        String authority = "";
        List<String> roleCode = userMapper.getRoleById(id);
        if (roleCode.size() > 0) {
            for (String s : roleCode) {
                authority += "ROLE_" + s + ",";
            }
        }

        List<String> perms = userMapper.getPermsById(id);

        if (perms != null) {
            for (String perm : perms) {
                authority += perm + ",";
            }
        }
        return authority;
    }

    @Override
    public List<User> getUserList(Map<String,Object> userMap) {
        List<User> userList = userMapper.getUserList(userMap);
        List<Role> userRole = new ArrayList<>();
        User newUser = new User();
        for (int i = 0;i < userList.size();i++) {
            newUser = userList.get(i);
            userRole = userMapper.getUserRole(newUser.getId());
            newUser.setRoleList(userRole);
            userList.set(i,newUser);
        }
        return userList;
    }

    @Override
    public User getUserInfo(Integer id) {
        return userMapper.getUserInfo(id);
    }

    @Override
    public int updateUser(Map<String, Object> userMap) {
        userMap.put("updateTime",new Date());
        return userMapper.updateUser(userMap);
    }

//    @Override
//    public int delUser(Integer id) {
//        return userMapper.delUser(id);
//    }

    @Override
    public List<Role> getUserRole(Integer id) {
        return userMapper.getUserRole(id);
    }

    @Override
    public List<Role> getAllRole() {
        return userMapper.getAllRole();
    }

    @Override
    public List<Integer> getRoleId(Integer id) {
        return userMapper.getRoleId(id);
    }

    @Override
    public int assignRole(Integer id, List<Integer> list) {
        List<Integer> roleId = getRoleId(id);
        if (roleId.size() > list.size()) {
            roleId.removeAll(list);
            return userMapper.delRole(id,roleId);
        } else if (roleId.size() < list.size()) {
            list.removeAll(roleId);
            return userMapper.addRole(id,list);
        } else {
            return 1;
        }
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    @Async
    public void sendEmail(String to, String subject, String content) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        mailSender.send(message);
    }
}
